package com.aji.oldwine.controller;

import com.aji.allcommon.result.Result;
import com.aji.allcommon.service.FileStorageService;
import com.aji.allcommon.utills.MultipartFileUtill;
import com.aji.oldwine.domain.Oldwine;
import com.aji.oldwine.domain.OldwineConditionDto;
import com.aji.oldwine.service.OldwineService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

@CrossOrigin
@Api(tags = "考古管理相关接口")
@RestController
@RequestMapping("/old")
@RequiredArgsConstructor
public class OldwineController {
    private final OldwineService oldwineService;
    private final FileStorageService fileStorageService;
    @ApiOperation("图片转url")
    @PostMapping("/imgToUrl")
    public Result imgToUrl(MultipartFile imgFile) throws IOException {
        String originalFilename = imgFile.getOriginalFilename();
        String simpFileName = originalFilename.substring(0, originalFilename.length() - 4) + ".jpg";
        File fileimg = File.createTempFile("temp", null);
        imgFile.transferTo(fileimg);
        FileInputStream simpJpgFile = MultipartFileUtill.multipartTifToJpgFileInputStream(fileimg);
        String accessSimpFileName = fileStorageService.uploadFile("img/jpg", simpFileName, simpJpgFile, "img/jpg");
        return Result.success(accessSimpFileName);
    }
    @ApiOperation("增加 需要忽略id")
    @PostMapping()
    public Result save(@RequestBody Oldwine entity){
        oldwineService.save(entity);
        return Result.success();
    }
    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        oldwineService.removeByIds(ids);
        return Result.success();
    }
    @ApiOperation("修改")
    @PutMapping()
    public Result updateById(@RequestBody Oldwine entity){
        System.out.println(entity);
        oldwineService.updateById(entity);
        return Result.success();
    }
    @ApiOperation("查询 根据id")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(oldwineService.getById(id));
    }
    @ApiOperation("查询 全部")
    @GetMapping("/list")
    public Result list(){
        return Result.success(oldwineService.list(null));
    }
    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{pageSize}")
    public Result listByConditionAndPage(@PathVariable int page, @PathVariable int pageSize, OldwineConditionDto compound){
        //合法检验
        if(page<1||pageSize<1){return Result.error("分页参数异常");}
        Page<Oldwine> compoundPage= new Page<>(page,pageSize);
        LambdaQueryWrapper<Oldwine> compoundLambdaQueryWrapper  =new LambdaQueryWrapper<Oldwine>().select()
                .like(compound.getArchId()!=null,Oldwine::getArchId,compound.getArchId())
                .like(compound.getCollector()!=null,Oldwine::getCollector,compound.getCollector())
                .like(compound.getLabId()!=null,Oldwine::getLabId,compound.getLabId())
                .like(compound.getClassThing()!=null,Oldwine::getClassThing,compound.getClassThing())
                ;
        IPage<Oldwine> compoundIPage = oldwineService.page(compoundPage, compoundLambdaQueryWrapper);
        return Result.success(compoundIPage);

    }

    @ApiOperation("获取数据总量")
    @GetMapping("/count")
    public Result count(){
        int count = Math.toIntExact(oldwineService.count());
        return Result.success(count);
    }

}
